<?php

namespace App\Admin\Controllers;

use App\Models\CoinClueGlobal;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CoinClueGlobalController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CoinClueGlobal(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
//            $grid->column('type');
            $grid->column('content', '内容')->if(function () {
                return $this->type == \App\Models\CoinClue::TYPE_IMAGE;
            })->image(50, 50)->else()->display(function () {
                return $this->content;
            });
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

//            $grid->filter(function (Grid\Filter $filter) {
//                $filter->equal('id');
//
//            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CoinClueGlobal(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type')->using(\App\Models\CoinClue::TYPE);
            $show->field('content', '内容')->as(function ($content) {
                return $this->type == \App\Models\CoinClue::TYPE_IMAGE ? '<img src="' . $content . '" style="width: 100px;height: 100px;">' : $content;
            })->unescape();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CoinClueGlobal(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->radio('type')
                ->options(\App\Models\CoinClue::TYPE)
                ->default(\App\Models\CoinClue::TYPE_TEXT)
                ->when(\App\Models\CoinClue::TYPE_TEXT, function (Form $form) {
                    $form->text('content', '内容');
                })->when(\App\Models\CoinClue::TYPE_IMAGE, function (Form $form) {
                    $form->image('image', '内容')->retainable()->removable(false)->autoUpload();
                    if ($form->model()->content) {
                        $form->html('<img src="' . $form->model()->content . '" height="200px" width="200px"/>');
                    }
                });
            $form->saving(function (Form $form) {
                if ($form->type == \App\Models\CoinClue::TYPE_IMAGE) {
                    $form->content = $form->image;
                }
                $form->deleteInput('image');
            });
            $form->display('created_at');
            $form->display('updated_at');

            $form->disableViewButton();
            $form->disableViewCheck();
            $form->disableCreatingCheck();
            $form->disableEditingCheck();
        });
    }
}
